//
//  ZKRecommendTableView.m
//  ZAKER
//
//  Created by yaohongchao on 16/2/28.
//  Copyright © 2016年 yaohongchao. All rights reserved.
//

#import "ZKVCRecommend.h"
#import "ZKRecommendTableViewCell.h"
#import "ZKRecommendModel.h"
#import "ZKVCSwipeDetail.h"

@interface ZKVCRecommend()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) ZKRecommendModel *model;
@end

@implementation ZKVCRecommend

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//http://iphone.myzaker.com/zaker/find.php?_appid=iphone&_version=6.46

-(void)viewDidLoad{
    [super viewDidLoad];
    [self setui];
    [self setdata];
}


-(void)setui{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;

}

-(void)setdata{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:@"http://iphone.myzaker.com/zaker/find.php?_appid=iphone&_version=7.4"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    WS(weakSelf);
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            weakSelf.model = [ZKRecommendModel mj_objectWithKeyValues:responseObject];
            [weakSelf.tableView reloadData];
        }
    }];
    [dataTask resume];
    
}

#pragma mark UITableViewDelegate UItableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.data.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZKRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReuseID"];
    if(!cell){
        cell = [[ZKRecommendTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ReuseID"];
    }
    List *list = [self.model.data.list objectWithIndex:indexPath.row];
    [cell refresh:list.block_info];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZKVCSwipeDetail *detail = [[ZKVCSwipeDetail alloc]init];
    List *list = [self.model.data.list objectWithIndex:indexPath.row];
    detail.apiUrl = list.block_info.api_url;
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)dealloc{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.tableView = nil;
}

@end



